# -*- coding: utf-8 -*-
import os

import wx

def decorateBmp(bmp,bmpDecoration):
    """
       为一个bmp贴上装饰图片,目前只能贴在左上角
      
      2.装饰图片是一个小图片:
       ----------    ---           ---------- 
       |        |    |X |          |X       |
       |original| +  ---         = |original|
       |        |                  |        | 
       ----------                  ----------
      
      args:
        wx.Bitmap bmp 被装饰的原始图片
        wx.Bitmap bmpDecoration bmp格式的装饰器件
      return wx.Bitmap ..装饰后的图像
    """
    result = wx.EmptyBitmap( bmp.GetWidth(), bmp.GetHeight())
    dc = wx.MemoryDC()
    dc.SelectObject(result)
    dc.Clear()
    dc.DrawBitmap(bmp, 0,0, True)
    dc.DrawBitmap(bmpDecoration, 0,0, True)
    
    del dc
    return result

 
def icon2Bmp(icon):
    """
    args:
      wx.Icon icon
    return wx.Bitmap
    """
    bmp = wx.EmptyBitmap( icon.GetWidth(), icon.GetHeight())
    bmp.CopyFromIcon( icon ) 
    return bmp

def icon2Image(icon):
    """
    args:
      wx.Icon icon
    return wx.Image
    """
    return icon2Bmp(icon).ConvertToImage() 

def image2Icon(image):
    """
    args:
      wx.Image image
    return wx.Icon
    """
    return bmp2icon(image.ConvertToBitmap())


def bmp2icon(bmp):
    """
    args:
      wx.Bitmap bmp
    return wx.Icon
    """
    result=wx.Icon("iconName",wx.BITMAP_TYPE_ICO,bmp.GetWidth(), bmp.GetHeight())
    result.CopyFromBitmap(bmp) 
    return result

def getFileIcon(fileExt):
    """
    getFileIcon(str fileExt) -> wx.Icon
    
     获取file扩展名相关的Icon
    """
    mimeTypeManager=wx.MimeTypesManager()
    defaultIcon=wx.ArtProvider_GetIcon(wx.ART_NORMAL_FILE,     wx.ART_OTHER, self.__imageSize)
    
    nolog=wx.LogNull() #可以抑制获取图标时弹出错误消息匡但无excpetion的情况,使用完请删除
                       #参考:http://blog.donews.com/limodou/archive/2005/12/26/671633.aspx 
    fileType=mimeTypeManager.GetFileTypeFromExtension( fileExt )
    
    if fileType is None:
        return defaultIcon
    
    iconInfo=fileType.GetIconInfo()
    if iconInfo is None:
        return defaultIcon
    
    icon = iconInfo[0]#icon, file, index = iconInfo
    if not icon.Ok():
       return defaultIcon
   
    del nolog
    return icon


class ImageLoader(object):
    def __init__(self,path):
        self.path=path
        
    def load(self,width=-1,height=-1):
        '''load(int width=-1,int height=-1) -> wx.Image
        
        如果提供width和heigth就把原始图片缩放到指定大小
             
       args: 
         width  缩放后的宽度
         heigth 缩放后的高度
         
       return:
          放大或缩小的一个copy
 
        '''
        assert os.path.exists(self.path) ,'image not find : %s' %self.path
        
        result = wx.Image(self.path ,wx.BITMAP_TYPE_ANY )
        if width != -1 and height!=-1:
            return result.Scale(width,height)
        return result
    
    def loadBmp(self,width=-1,height=-1):
        return self.load(width, height).ConvertToBitmap() 
